'use strict'
const path = require('path')

function resolve(dir) {
  return path.join(__dirname, dir)
}

console.log('---------vue.config.es5.js')


module.exports = {
  publicPath: './',
  // 如果你不需要生产环境的 source map，可以将其设置为 false 以加速生产环境构建。
  productionSourceMap: false,
  css: {
    extract: true, sourceMap: false,// css打包分离
    // extract: false, sourceMap: false,// css打包不分离
    //配置sass（css编译工具）
    loaderOptions: {
      scss: {
        additionalData: (content, loaderContext) => {
          // 有关可用属性的更多信息 https://webpack.js.org/api/loaders/
          const {resourcePath, rootContext} = loaderContext;
          const relativePath = path.relative(rootContext, resourcePath).replace(/\\/g, '/');
          if (relativePath === 'local_modules/ZhiXinLib/style/global.module.scss') {
            return content
          }
          return `@import "~@localModules/ZhiXinLib/style/global.module.scss";
          ${content}`
        }
      },
    }
  },
  // webpack-dev-server 相关配置
  devServer: {
    port: process.env.VUE_APP_PORT || 80,
    host: '0.0.0.0',
    headers: {
      // "Cross-Origin-Opener-Policy": "same-origin",
      // "Cross-Origin-Embedder-Policy": "require-corp",
      "X-Frame-Options": "ALLOW-FROM",
      "Cross-Origin-Opener-Policy": "same-origin",
      "Cross-Origin-Embedder-Policy": "require-corp",
    }
  },
  configureWebpack: {
    output: {
      libraryExport: 'default', // 增加这个属性，export default Pinia 这个就可以在浏览器中直接访问到Pinia了
      environment: {
        // 学习webpack5发现 babel的智能预设无法将箭头函数转换为普通函数
        arrowFunction: false
      },
    },
    experiments: {
      asyncWebAssembly: true,
    },
    resolve: {
      ...require('./webpack.config').resolve
    },
  },
  // 转换所有第三方包
  transpileDependencies: ['*'],
  chainWebpack(config) {
    config.plugins.delete('preload') // TODO: need test
    config.plugins.delete('prefetch') // TODO: need test

    config.module
      .rule('ts')
      .test(/\.(ts|tsx)$/)
      .use('babel-loader')
      .loader('babel-loader')
      .end()
      .use('ts-loader')
      .loader('ts-loader')
      .options({
        appendTsSuffixTo: [/\.vue$/],// 额外添加vue的支持
        transpileOnly: true, // 只用于编译
        happyPackMode: true, // 不检查类型
      })
      .end()

    config.module
      .rule('babel-loader')
      .test(/\.m?js$/)
      .use('babel-loader')
      .loader('babel-loader')
      .end()

    // set svg-sprite-loader
    config.module
      .rule('svg')
      .exclude.add(resolve('src/icons'))
      .end()
    config.module
      .rule('icons')
      .test(/\.svg$/)
      .include.add(resolve('src/icons'))
      .end()
      .use('svg-sprite-loader')
      .loader('svg-sprite-loader')
      .options({
        symbolId: 'icon-[name]'
      })
      .end()

    if (process.env.NODE_ENV === 'production') {
      // // 删除注释和console.log和debugger
      // // https://blog.csdn.net/weixin_45340607/article/details/129523343?spm=1001.2101.3001.6650.3&utm_medium=distribute.pc_relevant.none-task-blog-2%7Edefault%7EYuanLiJiHua%7EPosition-3-129523343-blog-118219834.235%5Ev38%5Epc_relevant_anti_vip_base&depth_1-utm_source=distribute.pc_relevant.none-task-blog-2%7Edefault%7EYuanLiJiHua%7EPosition-3-129523343-blog-118219834.235%5Ev38%5Epc_relevant_anti_vip_base&utm_relevant_index=4
      config.optimization.minimizer('terser').tap(args => {
        args.forEach(item => {
          if (item.hasOwnProperty('terserOptions')) {
            Object.assign(item['terserOptions'].compress, {
              drop_debugger: true,
              // drop_console: true,
              // pure_funcs: ['console.log']
            })
          }
          item['terserOptions']['format'] = {
            comments: false
          }
        })
        return args
      })
    }
  }
}
